package com.cybertron.ironhide.solr.test;

import java.io.IOException;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;

public class PositiveMatch {
	/**
	 * <p>
	 * Description：最大正向匹配算法
	 * 
	 * @throws IOException
	 * @author：
	 */
	public static void main(String[] args) throws IOException {
		int maxlen = 11;
		String str = "宝nike jordan宝:一寸光阴一寸金;";
		Vector dictionary = new Vector();
		dictionary = ReadFileToVector
				.ReadFile("D:\\var\\www\\resin\\solr\\words.dic");// 从文件中加载词典，结果放到vector向量中

		int i = 0, j = 0;
		for (i = 0; i < str.length(); i++) {
			if (str.length() - i == 1) {
				System.out.print(str.substring(i));
				break;
			} else if (str.length() - i < maxlen)
				maxlen = str.length() - i;

			j = i + maxlen - 1;
			String key = str.substring(i, j);
			if (dictionary.contains(key)) {
				System.out.print(key + "  ");
				i = i + key.length();
				continue;
			}
			while (key.length() > 1) {
				j--;
				key = str.substring(i, j);
				if (dictionary.contains(key)) {
					System.out.print(key + "  ");
					i = i + key.length();
					break;
				}

				else if (key.length() == 1) {
					/*
					 * try{ FileOutputStream bw=new
					 * FileOutputStream("D:\\dic2.txt",true);
					 * bw.write(("\r\n"+key).getBytes()); }catch(IOException
					 * e){}
					 */
					i = i + key.length();
					break;
				}
			}

		}
	}
}
